/*
* jlb
*/
package com.jlb.active.ctrl;

import com.alibaba.fastjson.JSON;
import com.jlb.active.entity.ActivePool;
import com.jlb.active.entity.ActivePoolState;
import com.jlb.active.service.ActivePoolSV;
import com.jlb.core.entity.BeanRet;
import com.jlb.core.entity.Page;
import com.jlb.core.tools.redis.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 激活币池 控制器
 *
 * @author jlb
 */
@RestController
@RequestMapping("/activePool")
@Slf4j
@Api(value = "激活币池控制器", description = "激活币池控制器")
public class ActivePoolCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private ActivePoolSV activePoolSV;

    /**
     * 查询ActivePool一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询ActivePool一个详情信息", notes = "查询ActivePool一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(java.lang.Long id) {
        if (id == null) {
            return null;
        }
        ActivePool activePool = activePoolSV.load(id);
        log.info(JSON.toJSONString(activePool));
        return BeanRet.create(true, "查询成功", activePool);
    }


    /**
     * 根据条件id查询ActivePool一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询ActivePool一个详情信息", notes = "查询ActivePool一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BeanRet loadById(@PathVariable java.lang.Long id) {
        if (id == null) {
            return null;
        }
        ActivePool activePool = activePoolSV.loadById(id);
        log.info(JSON.toJSONString(activePool));
        return BeanRet.create(true, "查询成功", activePool);
    }


    /**
     * 查询ActivePool信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询ActivePool信息集合", notes = "查询ActivePool信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态", paramType = "query"),
            @ApiImplicitParam(name = "createTimeBegin", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "createTimeEnd", value = "创建时间", paramType = "query")
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore ActivePool activePool, Integer curPage, Integer pageSize, ActivePoolState state) {
        Page<ActivePool> page = new Page<ActivePool>(pageSize, curPage);
        activePool.setState(state != null ? state.name() : null);
        List<ActivePool> activePools = activePoolSV.list(activePool, page.genRowStart(), page.getPageSize());
        page.setVoList(activePools);
        int total = activePoolSV.count(activePool);
        page.setTotalRow(total);

        return BeanRet.create(true, "查询成功", page);

    }


    /**
     * 查询ActivePool信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询ActivePool信息集合", notes = "查询ActivePool信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public BeanRet listByPk(java.lang.Long id, Integer curPage, Integer pageSize) {
        Page<ActivePool> page = new Page<ActivePool>(pageSize, curPage);
        List<ActivePool> activePools = activePoolSV.list(id, page.genRowStart(), page.getPageSize());
        page.setVoList(activePools);
        int total = activePoolSV.count(id);
        page.setTotalRow(total);

        return BeanRet.create(true, "查询成功", page);
    }

    /**
     * 统计ActivePool信息数量根据主键
     *
     * @return 总条数
     */
    @ApiOperation(value = " 统计ActivePool信息数量根据主键", notes = " 统计ActivePool信息数量根据主键")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query")
    })
    @GetMapping(value = "/count/by")
    @ResponseBody
    public Integer count(java.lang.Long id) {
        return activePoolSV.count(id);
    }

    /**
     * 统计ActivePool信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计ActivePool信息数量", notes = "统计ActivePool信息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "激活币编码", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：正常 Normal，已领取 Received，已使用 Used .", paramType = "query"),
            @ApiImplicitParam(name = "price", value = "价格", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query")
    })
    @GetMapping(value = "/count")
    @ResponseBody
    public Integer count(@ApiIgnore ActivePool activePool) {
        if (activePool == null) {
            return activePoolSV.count(new HashMap());
        } else {
            return activePoolSV.count(activePool);
        }
    }


    /**
     * 批量创建激活码
     *
     * @return BeanRet
     */
    @ApiOperation(value = "批量创建激活码", notes = "批量创建激活码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "num", value = "数量", paramType = "query"),
            @ApiImplicitParam(name = "price", value = "价格", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(Integer num, Double price) {
        if (num > 5000) {
            return BeanRet.create(false, "激活币创建数量不能太大");
        }
        activePoolSV.save(num, price);
        return BeanRet.create(true, "创建成功");
    }


    /**
     * 批量转给领导人
     *
     * @return BeanRet
     */
    @ApiOperation(value = "批量转给领导人", notes = "批量转给领导人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户", paramType = "query"),
            @ApiImplicitParam(name = "actives", value = "激活码：多个用','隔开", paramType = "query"),
    })
    @PostMapping("/batchTransfer")
    @ResponseBody
    public BeanRet batchTransfer(String account, String actives) {
//        activePoolSV.batchTransfer(account, actives);
        return BeanRet.create(true, "转账成功");
    }


}
